#include "Game.h"
#include "DSGraphicsRenderer.h"
#include "DisplayList.h"
#include "Player.h"
#include "MeshObject.h"
#include "SkyBox.h"
#include "File.h"
#include "ResourceManager.h"
#include "GameObjectFactory.h"
#include "ConfigFile.h"
#include <iostream>
#include <vector>

DisplayList planeList;

SkyBox skybox;

Game::Game() : m_nextGameObjectID(0)
{

}

void Game::AddGameObject(GameObject* pGO)
{
	m_gameObjects.insert(std::pair<int, GameObject*>(m_nextGameObjectID, pGO));
}

bool Game::Init()
{
	// load the resources needed for this state
	TheResourceManager::Instance()->LoadResources("assets/mainstateresources.txt");

	File fr;
	fr.Open("assets/mainstateobjects.txt");

	while(!fr.EndOfFile())
	{
		char type[50];
		fr.GetString(type);

		GameObject* newObject = TheGameObjectFactory::Instance()->Create(type);
		newObject->Load(&fr);

		AddGameObject(newObject);

		std::cout << "Created " << type << " Object with ID: " << m_nextGameObjectID << "\n";

		IncrementGameObjectID();
	}

	planeList.AddBeginQuads();
	planeList.AddVertexColour(255,255,255);
	planeList.AddNormals(0.0f, -1.0f, 0.0f);
	planeList.AddTexCoords(-128, -128);
	planeList.AddVertex(-100.0f, 0.0f, -100.0f);
	planeList.AddTexCoords(128, -128);
	planeList.AddVertex(100.0f, 0.0f, -100.0f);
	planeList.AddTexCoords(128, 128);
	planeList.AddVertex(100.0f, 0.0f, 100.0f);
	planeList.AddTexCoords(-128, 128);
	planeList.AddVertex(-100.0f, 0.0f, 100.0f);
	planeList.End();

	std::cout << "Game Init success\n";
	return true;
}

void Game::Update(float gameSpeed)
{
	EventHandler::Update();

	for(GameObjects::iterator it = m_gameObjects.begin(); it != m_gameObjects.end(); ++it)
	{
		GameObject* p = it->second;
		p->Update(gameSpeed);
	}
}

void Game::Draw(IGraphicsRenderer* renderer)
{
	renderer->StartRenderScene();

	for(GameObjects::iterator it = m_gameObjects.begin(); it != m_gameObjects.end(); ++it)
	{
		GameObject* p = it->second;
		p->Draw();
	}

	glPushMatrix();

	glScalef(20.0f, 1.0f, 20.0f);
	glTranslatef(0.0f, -4.0f, 0.0f);

	glBindTexture(0, TheResourceManager::Instance()->GetTexture("sand"));

	glCallList(&planeList.m_list[0]);

	glPopMatrix(1);

	//glPushMatrix();

	glScalef(2.0f,2.0f,2.0f);

	// Render the front quad
	glBindTexture(0, TheResourceManager::Instance()->GetTexture("crate"));
	glBegin(GL_QUADS);
	glTexCoord2f(0, 0); glVertex3f(  0.5f, -0.5f, -0.5f );
	glTexCoord2f(1, 0); glVertex3f( -0.5f, -0.5f, -0.5f );
	glTexCoord2f(1, 1); glVertex3f( -0.5f,  0.5f, -0.5f );
	glTexCoord2f(0, 1); glVertex3f(  0.5f,  0.5f, -0.5f );

	glEnd();

	// Render the left quad
	//glBindTexture(0, TheResourceManager::Instance()->GetTexture("left"));
	glBegin(GL_QUADS);
	glTexCoord2f(0, 0); glVertex3f(  0.5f, -0.5f,  0.5f );
	glTexCoord2f(1, 0); glVertex3f(  0.5f, -0.5f, -0.5f );
	glTexCoord2f(1, 1); glVertex3f(  0.5f,  0.5f, -0.5f );
	glTexCoord2f(0, 1); glVertex3f(  0.5f,  0.5f,  0.5f );
	glEnd();

	// Render the back quad
	//glBindTexture(0, TheResourceManager::Instance()->GetTexture("back"));
	glBegin(GL_QUADS);
	glTexCoord2f(0, 0); glVertex3f( -0.5f, -0.5f,  0.5f );
	glTexCoord2f(1, 0); glVertex3f(  0.5f, -0.5f,  0.5f );
	glTexCoord2f(1, 1); glVertex3f(  0.5f,  0.5f,  0.5f );
	glTexCoord2f(0, 1); glVertex3f( -0.5f,  0.5f,  0.5f );
	glEnd();

	// Render the right quad
	//glBindTexture(0, TheResourceManager::Instance()->GetTexture("right"));
	glBegin(GL_QUADS);
	glTexCoord2f(0, 0); glVertex3f( -0.5f, -0.5f, -0.5f );
	glTexCoord2f(1, 0); glVertex3f( -0.5f, -0.5f,  0.5f );
	glTexCoord2f(1, 1); glVertex3f( -0.5f,  0.5f,  0.5f );
	glTexCoord2f(0, 1); glVertex3f( -0.5f,  0.5f, -0.5f );
	glEnd();

	// Render the top quad
	//glBindTexture(0, TheResourceManager::Instance()->GetTexture("house"));
	glBegin(GL_QUADS);
	glTexCoord2f(0, 1); glVertex3f( -0.5f,  0.5f, -0.5f );
	glTexCoord2f(0, 0); glVertex3f( -0.5f,  0.5f,  0.5f );
	glTexCoord2f(1, 0); glVertex3f(  0.5f,  0.5f,  0.5f );
	glTexCoord2f(1, 1); glVertex3f(  0.5f,  0.5f, -0.5f );
	glEnd();

	// Render the bottom quad
	//glBindTexture(0, TheResourceManager::Instance()->GetTexture("house"));
	//glBindTexture(0, TheResourceManager::Instance()->GetTexture("bottom"));
	glBegin(GL_QUADS);
	glTexCoord2f(0, 0); glVertex3f( -0.5f, -0.5f, -0.5f );
	glTexCoord2f(0, 1); glVertex3f( -0.5f, -0.5f,  0.5f );
	glTexCoord2f(1, 1); glVertex3f(  0.5f, -0.5f,  0.5f );
	glTexCoord2f(1, 0); glVertex3f(  0.5f, -0.5f, -0.5f );
	glEnd();

	//glPopMatrix(1);
	

	renderer->EndRenderScene();
}